jQ(function(){
	initAddressSelection();
	initBulidFun();
});

function initAddressSelection(){
	jQ.ajax({
		type: "POST",
		url: "userAjax!loadAllProvinces.action",
		success:function(data){
			if(data.result){
				jQ("#provinceselect").empty();
				var html="";
				html+="<option value='0' type='0'>";
				html+="请选择";
				html+="</option>";
				for(var i=0;i<data.provinces.length;i++){
					var province=data.provinces[i];
					var id=province.id;
					var name=province.name;
					var type=province.type;
					html+="<option value='"+id+"' type='"+type+"'>";
					html+=name;
					html+="</option>";
				}
				jQ("#provinceselect").append(html);
				jQ("#provinceselect").change(onProvincesSelect);
			}else{
				alert("数据出错");
			}
		}
	});
}

function onProvincesSelect(){
	jQ("#districtselect").append("<option value='0' type='0'>请选择</option>");
	var id=eval(jQ(this).val());
	var type=eval(jQ(this).find("option:selected").attr("type"));
	if(id==0){
		return;
	}else{
		if(type==-1){
			var name=jQ(this).find("option:selected").text();
			jQ("#cityselect").empty().append("<option type='-1' id='"+id+"' value=0>"+name+"</option>");
			jQ.ajax({
				type: "POST",
				url: "userAjax!loadDistrict.action",
				data:{
					cityid:0,
					provinceid:id
				},
				success:function(data){
					if(data.result){
						jQ("#districtselect").empty();
						var html="";
						html+="<option value='0' type='0'>";
						html+="请选择";
						html+="</option>";
						for(var i=0;i<data.districts.length;i++){
							var districts=data.districts[i];
							var id=districts.id;
							var name=districts.name;
							var type=districts.type;
							html+="<option value='"+id+"' type='"+type+"'>";
							html+=name;
							html+="</option>";
						}
						jQ("#districtselect").append(html);
					}else{
						alert("数据出错");
					}
				}
			});
			return;
		}else{
			jQ.ajax({
				type: "POST",
				url: "userAjax!loadCityByProvinceid.action",
				data:{
					provinceid:id,
					type:type
				},
				success:function(data){
					if(data.result){
						jQ("#cityselect").empty();
						var html="";
						html+="<option value='0' type='0'>";
						html+="请选择";
						html+="</option>";
						for(var i=0;i<data.cities.length;i++){
							var city=data.cities[i];
							var cityid=city.id;
							var name=city.name;
							var type=city.type;
							html+="<option value='"+cityid+"' pid='"+id+"' type='"+type+"'>";
							html+=name;
							html+="</option>";
						}
						jQ("#cityselect").append(html);
						jQ("#cityselect").change(onCitesSelect);
					}else{
						alert("数据出错");
					}
				}
			});
		}
	}

}

function onCitesSelect(){
	var id=eval(jQ(this).val()); //cityid
	var pid=eval(jQ(this).find("option:selected").attr("pid"));
	jQ.ajax({
		type: "POST",
		url: "userAjax!loadDistrict.action",
		data:{
			cityid:id,
			provinceid:pid
		},
		success:function(data){
			if(data.result){
				jQ("#districtselect").empty();
				var html="";
				html+="<option value='0' type='0'>";
				html+="请选择";
				html+="</option>";
				for(var i=0;i<data.districts.length;i++){
					var districts=data.districts[i];
					var id=districts.id;
					var name=districts.name;
					var type=districts.type;
					html+="<option value='"+id+"' type='"+type+"'>";
					html+=name;
					html+="</option>";
				}
				jQ("#districtselect").append(html);
			}else{
				alert("数据出错");
			}
		}
	});
}

function initBulidFun(){
	jQ("#username_ipt").blur(function(){
		checkUsername();
	});
	jQ("#password_ipt").blur(function(){
		checkPWD();
	});
	jQ("#password_repeat_ipt").blur(function(){
		checkPWD2();
	});
	jQ("#name_ipt").blur(function(){
		checkName();
	});
	jQ("#email_ipt").blur(function(){
		checkEmail();
	});
	jQ("#website_ipt").blur(function(){
		checkBlog();
	});
	jQ("#verifycode_ipt").blur(function(){
		checkYzm();
	});
//	jQ("#verifycode_img").attr("src", jQ("#ctxVal").val()+"/verifycode.do" + "?time=" + (new Date()).valueOf());
	jQ("#verifycode_img").click(function(){
		jQ(this).attr("src", jQ("#ctxVal").val()+"/verifycode.do" + "?time=" + (new Date()).valueOf());
	});
	jQ("#verifycode_img").click();
	jQ("#verifycode_change").click(function(){
		jQ("#verifycode_img").attr("src", jQ("#ctxVal").val()+"/verifycode.do" + "?time=" + (new Date()).valueOf());
	});
	jQ("#submit_btn").click(doSubmit);

}

function checkUsername(fun){
	var _value = jQ.trim(jQ("#username_ipt").val());
	var usernamereg=/[a-zA-Z]+[0-9]*/;
	var _b=jQ("#username_ipt").nextAll("b");
	if(usernamereg.test(_value)){
		jQ.ajax({
			type: "POST",
			url: "userAjax!checkCCUser.action",
			data:{
				emailInput : _value
			},
			success:function(data){
				_b.removeClass().empty();
				if(data.result){
					_b.addClass("right");
					if(typeof(fun)=="function")
						fun();
				}else{
					_b.addClass("wrong").html("登录名已被占用,请重新输入");

				}
			}
		});
	}else{
		_b.removeClass().addClass("wrong").html("请输入正确的登录名");

	}
}
function checkPWD(fun){
	var regEx = /^[a-zA-Z0-9][a-zA-Z0-9_-]{5,20}$/;
	var _value = jQ.trim(jQ("#password_ipt").val());
	var _b=jQ("#password_ipt").nextAll("b");
	_b.removeClass().empty();
	if(!regEx.test(_value)){
		_b.addClass("wrong").html("数字、英文、下划线及中杠线5到20位的密码");
	}else{
		_b.addClass("right");
		if(typeof(fun)=="function")
			fun();
	}
}
function checkPWD2(fun){
	var pwd = jQ.trim(jQ("#password_ipt").val());
	var _value = jQ.trim(jQ("#password_repeat_ipt").val());
	var _b=jQ("#password_repeat_ipt").nextAll("b");
	_b.removeClass().empty();
	if(_value.length<=0){
		_b.addClass("wrong").html("请输入密码");
	}else if(pwd==_value){
		_b.addClass("right");
		if(typeof(fun)=="function")
			fun();
	}else{
		_b.addClass("wrong").empty().append("两次输入的密码必须一致");
	}
}

function checkName(fun){
	var regEx = /^[\u4e00-\u9fa5_a-zA-Z0-9-]{2,16}$/;// 姓名 2-16个
	var _value = jQ.trim(jQ("#name_ipt").val());
	var _b=jQ("#name_ipt").nextAll("b");
	_b.removeClass().empty();
	if(regEx.test(_value)&&(_value.length>=2&&_value.length<=16)){
		_b.addClass("right");
		if(typeof(fun)=="function")
			fun();
	}else{
		_b.addClass("wrong").html("名称由2-16个字符中文、数字、英文、下划线、中杠线组成");
	}
}
function checkEmail(fun){
	var _value = jQ.trim(jQ("#email_ipt").val());
	var _b=jQ("#email_ipt").nextAll("b");
	_b.removeClass().empty();
	if(isEmail(_value)){
		_b.addClass("right");
		if(typeof(fun)=="function")
			fun();
	}else if(_value.length>0){
		_b.addClass("wrong").html("请输入正确的邮箱地址");
	}
}
function checkBlog(fun){
	var _value = jQ.trim(jQ("#website_ipt").val());
	var _b=jQ("#website_ipt").nextAll("b");
	_b.removeClass().empty();
	if(_value.length>0){
		if(isURL(_value)){
			_b.addClass("right");
			if(typeof(fun)=="function")
				fun();
		}else{
			_b.addClass("wrong").html("请输入正确的网址");
		}
	}
}
function checkYzm(fun){
	var _value = jQ.trim(jQ("#verifycode_ipt").val());
	var _b=jQ("#verifycode_ipt").nextAll("b");
	_b.removeClass().empty();
	if(_value.length<0){
		_b.addClass("wrong").html("请输入验证码");
	}else{
		jQ.ajax({
			type: "POST",
			url: "userAjax!checkYzm.action",
			data:{
				vcodeInput : _value
			},
			success:function(data){
				if(data=="0"){
					_b.addClass("wrong").html("验证码错误,请重新输入");
				}else{
					_b.addClass("right");
					if(typeof(fun)=="function")
						fun();
				}
			}
		});
	}
}

function getPreAddress(){
	var pval=eval(jQ("#provinceselect").val());
	if(pval==0){
		return "0";
	}else{
		var ptype=eval(jQ("#provinceselect option:selected").attr("type"));
		if(ptype==-1){
			var cval="0";
			var dval=eval(jQ("#districtselect").val());
			if(dval==0){
				return ""+pval;
			}else{
				return pval+"_0_"+dval;
			}
		}else{
			var cval=eval(jQ("#cityselect").val());
			if(cval==0){
				return ""+pval;
			}else{
				var dval=eval(jQ("#districtselect").val());
				if(dval==0){
					return pval+"_"+cval;
				}else{
					return pval+"_"+cval+"_"+dval;
				}
			}
		}
	}
}

function doSubmit(){
	var _username = jQ.trim(jQ("#username_ipt").val());
	var _password = jQ.trim(jQ("#password_ipt").val());
	var _repwd=jQ.trim(jQ("#password_repeat_ipt").val());
	var _name = jQ.trim(jQ("#name_ipt").val());
	var _email = jQ.trim(jQ("#email_ipt").val());
	var _website = jQ.trim(jQ("#website_ipt").val());
	var _verifycode = jQ.trim(jQ("#verifycode_ipt").val());
	var pre_address=getPreAddress();
	var suf_address=jQ.trim(jQ("#address_ext_ipt").val());
	if(pre_address.length>0&&pre_address!='0'&&suf_address.length<=0){
		var _b=jQ("#address_ext_ipt").nextAll("b");
		_b.addClass("wrong").html("请输入详细地址");
		return;
	}else if(suf_address.length>50){
		var _b=jQ("#address_ext_ipt").nextAll("b");
		_b.addClass("wrong").html("详细地址请在50个字内");
		return;
	}
//	var _website = jQ.trim(jQ("#website_ipt").val());
	checkUsername(function(){
		checkPWD(function(){
			checkPWD2(function(){
				checkName(function(){
					checkEmail(function(){
						checkBlog(function(){
							checkYzm(function(){
								jQ.ajax({
									type : "POST",
									url : "userAjax!settle.action",
									data : {
										usernameInput : _username,
										emailInput:_email,
										pwdInput : _password,
										pwdInput2 : _repwd,
										nameInput : _name,
										vcodeInput : _verifycode,
										webSite:_website,
										preAddress:pre_address,
										sufAddress:suf_address
									},
									success : function(data){
										if(data.result){
//											jQ("#div_msg div.msg p").html(dataObj.result);
											jQ("#div_msg div.msg_btm").html("<input type='button' style='width:auto' value='首页' onclick='gotoLogin()'>");
											jQ("#div_msg").show();
											jQ("#register").hide();
										}else{
											jalert(data.msg);
										}
									}
								});
							});
						});
					});
				});
			});
		});
	});

}
function gotoLogin(){
	window.location.href=jQ("#ctxVal").val()+"/index.html";
}
function getParameter(name){
    var paramStr=location.search;
    if(paramStr.length==0)return null;
    if(paramStr.charAt(0)!='?')return null;
    paramStr=decodeURIComponent(paramStr);
    paramStr=paramStr.substring(1);
    if(paramStr.length==0)return null;
    var params=paramStr.split('&');
    var p = null;
    for(var i=0;i<params.length;i++){
        if(params[i].indexOf(name) >= 0){
            p = params[i].split('=');
            p = p[1];
        }
    }
    return p;
}
function jalert(info,LFun){
	jQ("#ALERT p").text(info);
	if(LFun!=null)jQ("#ALERT").bind("LFun",LFun);
	jQ("#ALERT").dialog('open');
	jQ("#ALERT").dialog("option","buttons",{
		"确定" : function(){
			jQ(this).trigger("LFun");
			jQ(this).dialog("close");
		}
	});
}
